package com.example.qianjun.ninetable;

import android.app.Activity;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qianjun on 2016/5/24.
 */
public class GlobalUtil {

    public static int width = 0;
    public static int height = 0;
    public static int TEXT_SIZE = 40;

    public static void getTextSize(Activity activity) {
        WindowManager windowManager = activity.getWindowManager();
        width = windowManager.getDefaultDisplay().getWidth();
        height = windowManager.getDefaultDisplay().getHeight();

        float ratioWidth = (float) width / 480;
        float ratioHeight = (float) height / 800;
        float RATIO = Math.min(ratioWidth, ratioHeight);


        //3.根据上一步计算出来的最小纵横比来确定字体的大小(假定在480*800屏幕下字体大小设定为35)

        TEXT_SIZE = Math.round(40 * RATIO);


    }

    //一共八种旋转，Ⅰ，Ⅱ，Ⅲ，Ⅳ顺时针与逆时针旋转

    //顺时针
    public static void rotateClockwiseFirst(int[][] table) {

        int i = table[0][0];
        table[0][0] = table[0][1];
        table[0][1] = table[1][1];
        table[1][1] = table[1][0];
        table[1][0] = i;

    }

    public static void rotateClockwiseSecond(int[][] table) {
        int i = table[1][0];
        table[1][0] = table[1][1];
        table[1][1] = table[2][1];
        table[2][1] = table[2][0];
        table[2][0] = i;

    }

    public static void rotateClockwiseThird(int[][] table) {

        int i = table[0][1];
        table[0][1] = table[0][2];
        table[0][2] = table[1][2];
        table[1][2] = table[1][1];
        table[1][1] = i;
    }

    public static void rotateClockwiseFourth(int[][] table) {
        int i = table[1][1];
        table[1][1] = table[1][2];
        table[1][2] = table[2][2];
        table[2][2] = table[2][1];
        table[2][1] = i;
    }

    //逆时针
    public static void rotateCountclockwiseFirst(int[][] table) {

        int i = table[0][0];
        table[0][0] = table[1][0];
        table[1][0] = table[1][1];
        table[1][1] = table[0][1];
        table[0][1] = i;
    }

    public static void rotateCountclockwiseSecond(int[][] table) {
        int i = table[1][0];
        table[1][0] = table[2][0];
        table[2][0] = table[2][1];
        table[2][1] = table[1][1];
        table[1][1] = i;

    }

    public static void rotateCountclockwiseThird(int[][] table) {

        int i = table[0][1];
        table[0][1] = table[1][1];
        table[1][1] = table[1][2];
        table[1][2] = table[0][2];
        table[0][2] = i;
    }

    public static void rotateCountclockwiseFourth(int[][] table) {
        int i = table[1][1];
        table[1][1] = table[2][1];
        table[2][1] = table[2][2];
        table[2][2] = table[1][2];
        table[1][2] = i;
    }

    /**
     * @param floor floor表示当前所在的层级，也就是所处的转动队列中的位置
     * @return
     */
    public static List<Integer> findBestWay(int floor) {

        int thisFloor = floor + 1;
        int[] score = caculateScore(CheckBoard.lookingTable, CheckBoard.compareTable);
        int minScore = findMinScore(score);
        List<Integer> list = new ArrayList<Integer>();
        int count = 0;
        for (int i = 0; i < 8; i++) {
            if (minScore == score[i]) {
                list.add(i);
            }
        }

        if (minScore == 0) {
            List<Integer> returnListMark = new ArrayList<Integer>();
            for (int i = 0; i < list.size(); i++) {
                returnListMark.add(CheckBoard.countFlag);
                CheckBoard.map.put(CheckBoard.countFlag, new int[thisFloor]);
                CheckBoard.map.get(CheckBoard.countFlag)[thisFloor - 1] = list.get(i);
                CheckBoard.countFlag++;
            }

            return returnListMark;
        } else {
            List<Integer> setListMark = new ArrayList<Integer>();

            for (int i = 0; i < list.size(); i++) {
                //面板旋转

                rotatePanle(list.get(i));

                List<Integer> listMark = new ArrayList<>();
                List<Integer> next = new ArrayList<>();
                for (int y = 0; i < next.size(); y++) {

                    listMark.add(next.get(y));
                }


                //面板反向旋转

                counterRotatePanle(list.get(i));

                for (int j = 0; j < listMark.size(); j++) {
                    setListMark.add(listMark.get(j));
                    CheckBoard.map.get(listMark.get(j))[thisFloor - 1] = list.get(i);
                }
            }

            return setListMark;
        }

    }

    private static void counterRotatePanle(Integer integer) {

        switch (integer) {
            case 0:
                rotateCountclockwiseFirst(CheckBoard.lookingTable);
                break;
            case 1:
                rotateCountclockwiseSecond(CheckBoard.lookingTable);
                break;
            case 2:
                rotateCountclockwiseThird(CheckBoard.lookingTable);
                break;
            case 3:
                rotateCountclockwiseFourth(CheckBoard.lookingTable);
                break;
            case 4:
                rotateClockwiseFirst(CheckBoard.lookingTable);


                break;
            case 5:
                rotateClockwiseSecond(CheckBoard.lookingTable);

                break;
            case 6:
                rotateClockwiseThird(CheckBoard.lookingTable);


                break;
            case 7:
                rotateClockwiseFourth(CheckBoard.lookingTable);


                break;

        }


    }

    private static void rotatePanle(Integer integer) {

        switch (integer) {
            case 0:
                rotateClockwiseFirst(CheckBoard.lookingTable);
                break;
            case 1:
                rotateClockwiseSecond(CheckBoard.lookingTable);
                break;
            case 2:
                rotateClockwiseThird(CheckBoard.lookingTable);
                break;
            case 3:
                rotateClockwiseFourth(CheckBoard.lookingTable);
                break;
            case 4:
                rotateCountclockwiseFirst(CheckBoard.lookingTable);
                break;
            case 5:
                rotateCountclockwiseSecond(CheckBoard.lookingTable);
                break;
            case 6:
                rotateCountclockwiseThird(CheckBoard.lookingTable);
                break;
            case 7:
                rotateCountclockwiseFourth(CheckBoard.lookingTable);
                break;

        }

    }


    public static int findMinScore(int[] s) {
        int minScore = 1000;
        for (int i = 0; i < s.length; i++) {
            if (minScore > s[i]) {
                minScore = s[i];
            }
        }

        return minScore;
    }

    public static int[] caculateScore(int[][] t1, int[][] t2) {

        int[] score = new int[8];
        int countScore = 0;
        int count = 0;
        int pointscore = 0;

        for (int x = 0; x < 8; x++) {

            rotatePanle(x);
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {

                    for (int m = 0; m < 3; m++) {
                        for (int n = 0; n < 3; n++) {
                            if (t1[i][j] == t2[m][n]) {
                                pointscore = Math.abs(i - m) + Math.abs(j - n);
                                countScore += pointscore;
                                break;
                            }
                        }
                    }

                }
            }

            score[count++] = countScore;
            countScore = 0;
            counterRotatePanle(x);
        }


        return score;
    }


}
